package com.chinaunicom.aware;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/12/9 11:18
 * Description:
 */
public class MyApplicationAware implements BeanFactoryAware, BeanNameAware, ApplicationContextAware, BeanClassLoaderAware {


	private ClassLoader classLoader;
	private BeanFactory beanFactory;
	private String beanName;
	private ApplicationContext context;

	@Override
	public void setBeanClassLoader(ClassLoader classLoader) {
		System.out.println("Aware set beanClassLoader。。。。");
		this.classLoader = classLoader;
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("Aware set beanFactory。。。。");
		this.beanFactory = beanFactory;
	}

	@Override
	public void setBeanName(String name) {
		System.out.println("Aware set beanName。。。。");
		this.beanName = name;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		System.out.println("Aware set applicationContext。。。。");
		this.context = applicationContext;
	}


	/**
	 * 在 bean 初始化完毕之后执行
	 */
	public void display() {
		System.out.println("beanName: " + beanName);
		System.out.println("classLoader: " + classLoader);
		System.out.println("beanFactory: " + beanFactory);
		System.out.println("applicationContext: " + context);

		System.out.println("是否单例: " + beanFactory.isSingleton(beanName));
		System.out.println("系统环境: " + context.getEnvironment());
	}
}
